//
//  OrderSelectCountView.m
//  RentTable
//
//  Created by mac on 2024/11/30.
//

#import "OrderSelectCountView.h"

@interface OrderSelectCountView ()

@property (nonatomic, strong) UILabel *countTitleLabel;
@property (nonatomic, strong) UIButton *minusButton;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UIButton *plusButton;

@end

@implementation OrderSelectCountView

- (instancetype)initWithFrame:(CGRect)frame initialCount:(NSInteger)initialCount {
    self = [super initWithFrame:frame];
    if (self) {
        self.count = initialCount;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    
    self.countTitleLabel = [[UILabel alloc] init];
    self.countTitleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    self.countTitleLabel.textColor = myRGBHex(0x333333);
    self.countTitleLabel.text = @"数量";
    self.countTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.countTitleLabel];
    [self.countTitleLabel sizeToFit];
    [self.countTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.leading.mas_equalTo(16);
        make.height.mas_equalTo(20);
    }];
    
    self.minusButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.minusButton setTitle:@"-" forState:UIControlStateNormal];
    self.minusButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.minusButton setTitleColor:myRGBHex(0x999999) forState:UIControlStateNormal];
    [self.minusButton addTarget:self action:@selector(minusButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    

    self.countLabel = [[UILabel alloc] init];
    self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.count];
    self.countLabel.font = [UIFont pingFangSCRegularWithSize:14];
    self.countLabel.textAlignment = NSTextAlignmentCenter;
    

    self.plusButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.plusButton setTitle:@"+" forState:UIControlStateNormal];
    [self.plusButton setTitleColor:myRGBHex(0x999999) forState:UIControlStateNormal];
    self.plusButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.plusButton addTarget:self action:@selector(plusButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    

    [self addSubview:self.minusButton];
    [self addSubview:self.countLabel];
    [self addSubview:self.plusButton];
    
    [self.plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.mas_equalTo(-20);
        make.width.height.equalTo(@22);
    }];

    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.trailing.equalTo(self.plusButton.mas_leading).offset(-13);
        make.width.equalTo(@20);
    }];
    
    [self.minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.trailing.equalTo(self.countLabel.mas_leading).offset(-13);
        make.width.height.equalTo(@22);
    }];
    

}

- (void)minusButtonTapped {
    if (self.count <= 1) {
        return;
    }
    self.count--;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.count];
    if (self.countChanged) {
        self.countChanged(self.count);
    }
}

- (void)plusButtonTapped {
    self.count++;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.count];
    if (self.countChanged) {
        self.countChanged(self.count);
    }
}

@end
